package com.nulldev.util.uri.server;

import java.io.File;

import com.nulldev.util.io.FileUtil;
import com.nulldev.util.logging.LoggerFactory;

public abstract class HttpPage {

	private String data = "";
	private final String id;
	private final File path;
	
	private boolean allowCrossOrigin;
	
	public boolean canAccessCrossOrigin() {
		return this.allowCrossOrigin;
	}
	
	public void allowCrossOrigin(final boolean value) {
		this.allowCrossOrigin = value;
	}
	
	private boolean disableAccess;
	/**
	 * HttpPage for HttpServer
	 * @param id = "/" , "/page1", "/page2.html"
	 */
	public HttpPage(final String id, final File path) {
		this.id = id.replaceAll(" ", "%20");
		this.path = path;
	}
	
	public HttpPage(final String id) {
		this.id = id.replaceAll(" ", "%20");
		this.path = null;
	}
	
	private String overridenMime;
	public boolean isCustomMime() {
		return overridenMime != null && !overridenMime.equals("") && overridenMime.contains("/");
	}
	
	public String getMime() {
		return this.overridenMime;
	}
	
	public void setMime(final String mime) {
		this.overridenMime = mime;
	}
	
	public void setSendRaw(final boolean sendRaw) {
		if (this.path == null) {
			return;
		}
		this.sendRaw = sendRaw;
	}
	
	public boolean isDisabled() {
		return this.disableAccess;
	}
	
	public void setAccessDisabled(final boolean state) {
		this.disableAccess = state;
	}
	
	public void loadFromFile(final File f) throws Exception {
		this.data = FileUtil.readFileToString(f);
	}
	
	public String getID() {
		if (this.id == null) {
			return "";
		}
		return this.id;
	}
	
	public void setData(final String data) {
		this.data = data;
	}
	
	public String getData() {
		if (this.disableAccess) {
			return "<html><head><h1 style='font-family:sans-serif;'>403: Access Denied</h1><p style=\"font-family:sans-serif\">nullUtil Http Server 1.1 | Delivered by Thread #" + Thread.currentThread().getId() + "</p></head></html>";
		}
		return this.data;
	}
	
	public abstract String handleRequest(final String args, final String ip, final RequestType type);
	
	public abstract byte[] handleRequest_Alt(final String args, final String ip, final RequestType type);
	
	private boolean reply_bytes;
	public void replyOverBytes(final boolean reply_bytes) {
		this.reply_bytes = reply_bytes;
		this.sendRaw = false;
	}
	
	public int getLength() {
		return this.data.length();
	}
	
	public File getPath() {
		if (this.path == null) {
			LoggerFactory.getLogger().warn("getPath() -> path == null!");
		}
		return this.path;
	}

	private boolean sendRaw;
	public boolean sendRaw() {
		if (this.path == null) {
			return false;
		}
		return this.sendRaw;
	}

	public boolean isOverBytes() {
		return this.reply_bytes;
	}
	
	public static final HttpPage _NOT_FOUND_404 = new HttpPage("/error/_404") {
		
		@Override
		public byte[] handleRequest_Alt(String args, String ip, RequestType type) {
			return null;
		}
		
		@Override
		public String handleRequest(String args, String ip, RequestType type) {
			return "<html><head><h1 style='font-family:sans-serif;'>404: Page Not Found</h1><p style=\"font-family:sans-serif\">nullUtil Http Server 1.1 | Delivered by Thread #" + Thread.currentThread().getId()+ ".</p></head></html>";
		}
	};
	
	public HttpPage redirectTo(final HttpServer server, final HttpPage page) {
		final HttpPage find = server.getPage(page.getID());
		if (find == null) {
			return _NOT_FOUND_404;
		}else {
			return find;
		}
	}
}
